package com.hisense.hiioc.hismartplate.service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

/**
 * @Auther:liaohongbing@hisense.com
 * @date:2019/5/7
 * @des 获取环境天气数据
 */
@Service
public class WeatherDataService {
    private static final String weatherUrl = "https://free-api.heweather.net/s6/weather?location=南明,贵阳&key=7fecc6f4be2448f3ba43c6ec73d5ae28";
    private static final String airUrl = "https://free-api.heweather.net/s6/air/now?location=贵阳&key=7fecc6f4be2448f3ba43c6ec73d5ae28";
    private static final String alarmUrl = "https://api.heweather.net/s6/alarm?location=CN101260111&key=b4338d3733404574843551eb31cb9b1d";

    // 贵阳南明  CN101260111 //   测试辽宁CN101070208 //

    @Autowired
    RestTemplate restTemplate;


    public JSONArray getDailyForecast() {
        JSONArray forecastList = new JSONArray();
        try {
            forecastList = restTemplate.getForEntity(weatherUrl, JSONObject.class).getBody()
                    .getJSONArray("HeWeather6").getJSONObject(0).getJSONArray("daily_forecast");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return forecastList;
    }

    public JSONArray getLifestyle(){
        JSONArray lifeList = new JSONArray();
        try {
            lifeList = restTemplate.getForEntity(weatherUrl, JSONObject.class).getBody().
                    getJSONArray("HeWeather6").getJSONObject(0).getJSONArray("lifestyle");
        }catch (Exception e ){
            e.printStackTrace();
        }
        return lifeList;

    }

    public JSONObject getEnvironmentInfo(){

        JSONObject res = new JSONObject();
        JSONArray forecastList = new JSONArray();
        try {
            forecastList = restTemplate.getForEntity(weatherUrl, JSONObject.class).getBody()
                    .getJSONArray("HeWeather6").getJSONObject(0).getJSONArray("daily_forecast");
        } catch (Exception e) {
            e.printStackTrace();
        }

        JSONArray lifeList = new JSONArray();
        try {
            lifeList = restTemplate.getForEntity(weatherUrl, JSONObject.class).getBody().
                    getJSONArray("HeWeather6").getJSONObject(0).getJSONArray("lifestyle");
        }catch (Exception e ){
            e.printStackTrace();
        }

        JSONArray alarmList = new JSONArray();

        try {
            JSONObject alarmInfo = restTemplate.getForEntity(alarmUrl, JSONObject.class).getBody().
                    getJSONArray("HeWeather6").getJSONObject(0);
            if (alarmInfo.containsKey("alarm")){
                alarmList = alarmInfo.getJSONArray("alarm");
            }else{
                JSONArray list = new JSONArray();
                JSONObject nullAlarm = new JSONObject();
                nullAlarm.put("txt","无预警信息");
                list.add(nullAlarm);
                alarmList = list;
            }

        }catch (Exception e){
            e.printStackTrace();
        }

        //今日+空气
        JSONObject todayInfo = new JSONObject();
        JSONObject airInfo = new JSONObject();
        try {

            todayInfo = JSONObject.parseObject(forecastList.getJSONObject(0).toJSONString());
            airInfo = restTemplate.getForEntity(airUrl, JSONObject.class).getBody()
                    .getJSONArray("HeWeather6").getJSONObject(0).getJSONObject("air_now_city");
            for (String key : airInfo.keySet()){
                todayInfo.put(key,airInfo.getString(key));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }




        res.put("forecast",forecastList);
        res.put("lifeStyle",lifeList);
        res.put("alarm",alarmList);
        res.put("today",todayInfo);

        return res;

    }
}
